/**
 * Created by wujiankun on 2016/12/1.
 */
const common = require('./commonUtil.js');
const {loginToServer,} = require('./netUtils.js');
function doLogin(encryptedData,iv,callback){
    wx.showLoading({
        title: '请稍等...',
    });

    wx.checkSession({
        success () {
            //session_key 未过期，并且在本生命周期一直有效
            //发起网络请求
            let code =  common.storage.getFromLocal('jsCode');
            if(code){
                loginToServer({jsCode:code,encryptedData,iv},res=>{
                    console.log({type:'本地登录后返回数据',res});
                    if(res.status!=='200'){
                        common.toast(res.msg,'none');
                    }else{
                        common.storage.saveToLocal('unionid',res.data.unionId);
                        common.storage.saveToLocal('userInfo',JSON.stringify(res.data));
                        callback();
                    }
                    wx.hideLoading();
                });
            }else{
                goWxReLogin(encryptedData,iv,callback);
            }
        },
        fail () {
            // session_key 已经失效，需要重新执行登录流程
           goWxReLogin(encryptedData,iv,callback);
        }
    });
}

function goWxReLogin(encryptedData,iv,callback){
    wx.login({
        success (res) {
            if (res.code) {
                //发起网络请求
                loginToServer({jsCode:res.code,encryptedData,iv},res=>{
                    wx.hideLoading();
                    console.log({type:'本地登录后返回数据',res});
                    /*data:null
                    msg:"传入参数错误"
                    status:"501"*/
                    if(res.status!=='200'){
                        common.toast(res.msg,'none');
                    }else{
                        common.storage.saveToLocal('unionid',res.data.unionId);
                        common.storage.saveToLocal('userInfo',JSON.stringify(res.data));
                        common.storage.saveToLocal('jsCode',res.code);
                        callback();
                    }
                });
            } else {
                wx.hideLoading();
                console.log('登录失败！' + res.errMsg);
                common.toast(res.errMsg,'none');
            }
        },
        fail:function () {
            wx.hideLoading();
            common.toast('登录失败！','none');
        }
    })
}

function checkAndGetLocAuthorize(cb) {
    wx.getSetting({
        success: res => {
            let auth = res.authSetting;
            if (!auth['scope.userLocation']) {
                wx.authorize({
                    scope: 'scope.userLocation',
                    success(e) {
                        if (cb) {
                            cb(true);
                        }
                    },
                    fail(e) {
                        if (cb) {
                            cb(false);
                        }
                    }
                });
            }else{
                if (cb) {
                    cb(true);
                }
            }
        }
    });
}

module.exports = {
    doLogin,checkAndGetLocAuthorize
};